package czk.concrent.threadpool.newCachedThreadPool;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列的测试
 * @Author:ChenZhangKun
 * @Date: 2020/6/30 22:28
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        SynchronousQueue<Integer> synchronousQueue=new SynchronousQueue<>();
        // 线程一存储任务
        new Thread(()->{
            System.out.println("1 正在放入阻塞队列");
            try {
                // 阻塞，直到有线程来取
                synchronousQueue.put(1);
                System.out.println("1已经放入了阻塞队列");
                System.out.println("2正在放入阻塞队列");
                // 阻塞，直到有线程来取
                synchronousQueue.put(2);
                System.out.println("2已经放入阻塞队列");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        // 主线程婷一秒
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            // 线程二去取
            System.out.println("线程2去取了");
            try {
                // 去取阻塞队列的值
                synchronousQueue.take();
                System.out.println("线程二取到了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        // 线程三去取
        new Thread(()->{
            System.out.println("线程三去取");
            try {
                // 去取阻塞队列的值
                synchronousQueue.take();
                System.out.println("线程三取到了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
